<?php
include_once("../includes/page_utf_class.php");
include("../lang/cn/company_type_config.php");

/***********操作*************/
if($_GET['type'] && $_GET['id']>0){
	$sql = "select discription from ".TARENTOWITHDRAWAPPLY." where id='{$_GET['id']}'";
	$db->query($sql);
	$admin_remark = $db->fetchField('discription');
	$tpl->assign('type',$_GET['type']);
	$tpl->assign('id',$_GET['id']);
	$tpl->assign('admin_remark',$admin_remark);
	$tpl->assign('bank_en_abbr',$bank_en_abbr);
	$tpl->display('withdraw_apply_opear.htm');
	exit;
}

if($_POST['type'] && $_POST['id']>0){
	$status = "";
	$set = "";
	switch ($_POST['type'])
	{
		case 'pass':
			$status = 1;break;
		case 'not_pass':
			$status = -2;
			$sql = "select apply_money,member_id from ".TARENTOWITHDRAWAPPLY." where id='{$_POST['id']}' and status=0";
			$db->query($sql);
			$memInfo = $db->fetchRow();
			//给用户推送消息
			$replaceArr = [
			    'message'=>[
			        '{{reason}}' =>$_POST['admin_remark']
			    ]
			];
			$param = [
			    'memberId'=>$memInfo['member_id'],
			    'money'=>$config['money'].' '.$memInfo['apply_money'],
			    'time'=>date('Y-m-d H:i:s')
			];
			include_once($config["webroot"]."/lib/Helper.php");
			Helper::pushMessage('withdrawFaild', $replaceArr, $param);
			break;
		case 'complete':
			$status = 9;
			$sql = "select apply_money,member_id from ".TARENTOWITHDRAWAPPLY." where id='{$_POST['id']}' and status=1";
			$db->query($sql);
			$memInfo = $db->fetchRow();
			//给用户推送消息
			$replaceArr = [
			    'message'=>[
			        '{{tradeNo}}' =>$_POST['transaction_serial_number']
			    ]
			];
			$param = [
			    'memberId'=>$memInfo['member_id'],
			    'money'=>$config['money'].' '.$memInfo['apply_money'],
			    'timet'=>date('Y-m-d H:i:s')
			];
			include_once($config["webroot"]."/lib/Helper.php");
			Helper::pushMessage('withdrawSuccess', $replaceArr, $param);
			break;
		case 'cancel':
			$status = -1;break;
		default:
			break;
	}
	if(in_array($_POST['type'], array('not_pass','cancel'))){//审核不通过或取消，返还金额
		$opearStr = $_POST['type']=='not_pass'?"审核不通过":"取消";
		$sql = "select apply_money,member_id from ".TARENTOWITHDRAWAPPLY." where id='{$_POST['id']}' and status=0";
		$db->query($sql);
		$res = $db->fetchRow();
		if($res){
			//返还金额到账户
			$sql = "update ".TARENTO." set account=account+{$res['apply_money']} where member_id='{$res['member_id']}'";
			$db->query($sql);
			//写入收支明细
			$sql = "insert into ".TARENTOCASHFLOW." (`member_id`,`price`,`description`) values ({$res['member_id']},{$res['apply_money']},'提现申请{$opearStr}')";
			$db->query($sql);
		}
	}
	if($status){
		$set .=" set status='{$status}'";
		if($_POST['admin_remark']!=""){
			$set .= ",discription='{$_POST['admin_remark']}' ";
		}
		if($status==9){
			$set .= ",money_account_type='".strtolower($_POST['money_account_type'])."',money_account_number='".$_POST['money_account_number']."',transaction_serial_number='".$_POST['transaction_serial_number']."',finish_time='".time()."',finish_user='{$_SESSION['ADMIN_USER']}'";
		}else{
			$set .= ",examine_time='".time()."',examine_user='{$_SESSION['ADMIN_USER']}'";
		}
		$sql = "update ".TARENTOWITHDRAWAPPLY." {$set} where id='{$_POST['id']}'";
		$db->query($sql);
	}
	msg("module.php?m=product&s=withdraw_apply.php");
}
/**************操作******************/

$where = "";
if($_GET['rec_member_id']){
	$where .= " and a.member_id={$_GET['rec_member_id']}";
}
if($_GET['status']!=""){
	$where .= " and a.status='{$_GET['status']}'";
}

$sql = "select a.*,b.relations,c.name,c.mobile from ".TARENTOWITHDRAWAPPLY." a left join ".TARENTO." b on a.member_id=b.member_id left join ".MEMBER." c on a.member_id=c.userid where 1 {$where}";

//==============分页===============
$page = new Page;
$page->listRows=20;
if (!$page->__get('totalRows')){
	$db->query($sql);
	$de['count']=$page->totalRows = $db->num_rows();
}
$de['count']=$de['count']?$de['count']:$_GET['totalRows'];
$sql .= "  limit ".$page->firstRow.",".$page->listRows;
$de['page'] = $page->prompt();
//=============================

$db->query($sql);
$de['list'] = $db->getRows();

foreach ($de['list'] as $k=>$val)
{
	$de['list'][$k]['status_name'] = $withdraw_apply_status[$val['status']];
	$de['list'][$k]['member_type'] = $val['relations']?'合伙人':'普通用户';
	$de['list'][$k]['account_type_str'] = $bank_en_abbr[strtoupper($val['account_type'])];
	$de['list'][$k]['money_account_type_str'] = $bank_en_abbr[strtoupper($val['money_account_type'])];
}

$tpl->assign('de',$de);
$tpl->assign('withdraw_apply_status',$withdraw_apply_status);
$tpl->display("withdraw_apply.htm");
?>